package com.oig.sys.auth.controller;

import com.oig.common.exception.BadRequestException;
import com.oig.common.module.SysRequest;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.auth.dto.RoleDto;
import com.oig.sys.auth.service.ITbRoleService;
import com.oig.sys.auth.vo.RoleVo;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "7-角色权限管理")
@RestController
@RequestMapping("/role")
@Slf4j
public class TbRoleController {

    private final ITbRoleService roleService ;

    @Autowired
    public TbRoleController(ITbRoleService roleService){
        this.roleService = roleService;
    }


    @PreAuthorize("@pms.hasPermission('/role/addRole')")
    @SysLog(value="添加角色",apiUrl="/role/addRole")
    @Operation(summary = "添加角色,可以批量增加角色的子应用、菜单、接口权限等")
    @PostMapping("/addRole")
    public RoleVo addRole(@RequestBody @Validated(GroupAdd.class) RoleDto roleDto){
        log.info("add role : {}", roleDto);
        return this.roleService.addRole(roleDto) ;
    }


    @PreAuthorize("@pms.hasPermission('/role/updateRole')")
    @SysLog(value="修改角色",apiUrl="/role/updateRole")
    @Operation(summary = "修改角色,可以批量增加角色的子应用、菜单、接口权限等")
    @PostMapping("/updateRole")
    public RoleVo updateRole(@RequestBody @Validated(GroupEdit.class) RoleDto roleDto){
        log.info("update role : {}", roleDto);
        return this.roleService.updateRole(roleDto) ;
    }

    @PreAuthorize("@pms.hasPermission('/role/delRole')")
    @SysLog(value="物理删除角色",apiUrl="/role/delRole")
    @Operation(summary = "物理删除角色,直接删除所有角色关联数据，一定要二次确认")
    @PostMapping(value = "/delRole/{roleId}")
    public SysResponse<Integer> delRole(@PathVariable("roleId") Integer roleId){
        log.info("del Role roleId:{}",roleId);
        this.roleService.delRole(roleId);
        return SysResponse.ok(roleId,"删除成功");
    }

    @PreAuthorize("@pms.hasPermission('/role/addRoleSystem')")
    @SysLog(value="角色添加应用权限",apiUrl="/role/addRoleSystem")
    @Operation(summary = "角色添加应用权限,单独批量加子应用权限")
    @PostMapping("/addRoleSystem/{roleId}")
    public SysResponse<Integer> addRoleSystem(@PathVariable("roleId")Integer roleId, @RequestBody List<Integer> sysIdList){
        log.info("add role system: roleId={},systemId={}", roleId, sysIdList);
        this.roleService.addRoleSystem(roleId, sysIdList);
        return SysResponse.ok(roleId,"成功");
    }

    @PreAuthorize("@pms.hasPermission('/role/delRoleSystem')")
    @SysLog(value="物理删除角色的应用权限",apiUrl="/role/delRoleSystem")
    @Operation(summary = "物理删除角色的应用权限")
    @PostMapping("/delRoleSystem/{roleId}")
    public SysResponse<Integer> delRoleSystem(@PathVariable("roleId")Integer roleId, @RequestBody List<Integer> sysIdList){
        log.info("del role system: roleId={},systemId={}", roleId, sysIdList);
        this.roleService.delRoleSystem(roleId, sysIdList);
        return SysResponse.ok(roleId,"成功");
    }

    @PreAuthorize("@pms.hasPermission('/role/addRoleResource')")
    @SysLog(value="角色添加菜单权限",apiUrl="/role/addRoleResource")
    @Operation(summary = "角色添加菜单权限,单独批量添加菜单权限")
    @PostMapping("/addRoleResource/{roleId}")
    public SysResponse<Integer> addRoleResource(@PathVariable("roleId")Integer roleId, @RequestBody List<Integer> resourceIdList){
        log.info("add role resource: roleId={},resourceId={}", roleId, resourceIdList);
        this.roleService.addRoleResource(roleId, resourceIdList);
        return SysResponse.ok(roleId,"成功");
    }

    @PreAuthorize("@pms.hasPermission('/role/delRoleResource')")
    @SysLog(value="物理删除角色的菜单权限",apiUrl="/role/delRoleResource")
    @Operation(summary = "物理删除角色的菜单权限")
    @PostMapping("/delRoleResource/{roleId}")
    public SysResponse<Integer> delRoleResource(@PathVariable("roleId")Integer roleId, @RequestBody List<Integer> resourceIdList){
        log.info("del role resource: roleId={},resourceId={}", roleId, resourceIdList);
        this.roleService.delRoleResource(roleId, resourceIdList);
        return SysResponse.ok(roleId,"删除成功");
    }

    @PreAuthorize("@pms.hasPermission('/role/addRolePermission')")
    @SysLog(value="角色添加接口权限",apiUrl="/role/addRolePermission")
    @Operation(summary = "角色添加接口权限,单独批量增加接口权限")
    @PostMapping("/addRolePermission/{roleId}")
    public SysResponse<Integer> addRolePermission(@PathVariable("roleId")Integer roleId, @RequestBody List<Integer> permissionIdList){
        log.info("add role Permission: roleId={},permissionId={}", roleId, permissionIdList);
        this.roleService.addRolePermission(roleId, permissionIdList);
        return SysResponse.ok(roleId,"成功");
    }

    @PreAuthorize("@pms.hasPermission('/role/delRolePermission')")
    @SysLog(value="物理删除角色的接口权限",apiUrl="/role/delRolePermission")
    @Operation(summary = "物理删除角色的接口权限")
    @PostMapping("/delRolePermission/{roleId}")
    public SysResponse<Integer> delRolePermission(@PathVariable("roleId")Integer roleId, @RequestBody List<Integer> permissionIdList){
        log.info("del role Permission: roleId={},permissionId={}", roleId, permissionIdList);
        this.roleService.delRolePermission(roleId, permissionIdList);
        return SysResponse.ok(roleId,"成功");
    }


    @Operation(summary = "查询角色,用于给用户或者岗位配置角色，根据租户查询，租户id必传，并且只返回当前用户所拥有的角色")
    @PostMapping(value = "/listAllRole")
    public List<RoleVo> listAllRole(@RequestBody SysRequest<RoleDto> roleRequest){
        log.info("list all role, {}", roleRequest);
        if (roleRequest.getParamDto().getTenantId()==null){
            roleRequest.getParamDto().setTenantId(SecurityUtil.getDefineUserMain().getTenantId());
        }
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){ //租户，要校验数据权限
            if (!roleRequest.getParamDto().getTenantId().equals(SecurityUtil.getDefineUserMain().getTenantId())){
                throw new BadRequestException("orgzId参数错误，不是租户或无租户权限");
            }
        }
        return this.roleService.listAllRole(roleRequest);
    }


    @Operation(summary = "查看,查看详情，如果有子表则包含子表信息")
    @PostMapping(value = "/info/{roleId}")
    public RoleVo info(@PathVariable("roleId") Integer roleId){
        log.info("info RoleVo id={}", roleId) ;
        return this.roleService.getRoleInfo(roleId);
    }

}
